﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Threading;
//
namespace iarcc
{
  class Program
  {
    static string ccpatch;
    static string ccopt;
    static string asmpatch;
    static string asmopt;
    static string makepatch;
    static string makeopt;
    static string hexpatch;
    static string hexopt;
    static string gdbserpatch;
    static string gdbseropt;
    static string gdbpatch;
    static string gdbopt;
    static bool readcof()
    {
      int a;
      //读取配置
      string filepatch = "iar.cof";
      if (File.Exists(filepatch) == false)
      {
        Console.Write("配置文件错误");
        return false;
      }
      StreamReader objReader = new StreamReader(filepatch);
      ArrayList arrText = new ArrayList();
      string sLine = "";
      while (sLine != null)
      {
        sLine = objReader.ReadLine();
        if (sLine != null)
          arrText.Add(sLine);
      }
      objReader.Close();
      //1 cc 编译器路径
      ccpatch = (string)arrText[0];
      //cc 编译选项
      ccopt = "";
      for (a = 1; (string)arrText[a] != "End"; a++)
        ccopt = ccopt + (string)arrText[a] + " ";
      //2 asm 编译路径
      a++;
      asmpatch = (string)arrText[a++];
      //asm 选项
      asmopt = "";
      for (; (string)arrText[a] != "End"; a++)
        asmopt = asmopt + (string)arrText[a] + " ";
      //-make 编译路径
      a++;
      makepatch = (string)arrText[a++];
      //make 参数
      makeopt = "";
      for (; (string)arrText[a] != "End"; a++)
        makeopt = makeopt + (string)arrText[a] + " ";
      //-make 编译路径
      a++;
      hexpatch = (string)arrText[a++];
      //make 参数
      hexopt = "";
      for (; (string)arrText[a] != "End"; a++)
        hexopt = hexopt + (string)arrText[a] + " ";
      //gdbsevr 路径
      a++;
      gdbserpatch = (string)arrText[a++];
     //gdb sevr参数
      gdbseropt = "";
      for (; (string)arrText[a] != "End"; a++)
        gdbseropt = gdbseropt + (string)arrText[a] + " ";
      //gdb 路径
      a++;
      gdbpatch = (string)arrText[a++];
      //gdb 参数
      gdbopt = "";
      for (; (string)arrText[a] != "End"; a++)
        gdbopt = gdbopt + (string)arrText[a] + " ";
      //
      return true;
    }
    static void Main(string[] args)
    {
      //拼装参数/检查文件类型
      string argstr = "";
      string objft, tmpstr1;
      objft = "";
      foreach (string s in args)
      {
        argstr += s + " ";
        if (s.LastIndexOf('.') > 0 && objft == "")
        {
          tmpstr1 = s.Substring(s.LastIndexOf('.') + 1);
         if (s.Substring(0, 1) != "-")
         {
           if(tmpstr1 == "c" || tmpstr1 == "cpp" || tmpstr1 == "asm")
             objft = tmpstr1;
         }
        }
      }
      
      //读取配置
      if (readcof() == false)
        return;
      //调用编译器
      argstr = argstr.Replace("-c ", "");
      //argstr = argstr.Replace("-o ", "");
      Process mpro = new Process();
      //-make
      if (argstr.IndexOf("-make") > -1)
      {
        argstr = argstr.Replace("-make ", "");
        mpro.StartInfo.FileName = makepatch;
        mpro.StartInfo.Arguments = argstr + makeopt;
        Console.Write("准备创建目标\n");
        //
        Console.Write(mpro.StartInfo.Arguments+"\n");
        mpro.StartInfo.UseShellExecute = false;
        mpro.Start();
        mpro.WaitForExit();
        mpro.Close();
        //
        mpro.StartInfo.FileName = hexpatch;
        mpro.StartInfo.Arguments = hexopt;
        //
        Console.Write(mpro.StartInfo.Arguments+"\n");
        //
        mpro.Start();
        mpro.WaitForExit();
        mpro.Close();
        return;
      }
      //-gdb
      else if (argstr.IndexOf("-gdb") > -1)
      {
        Process gdbserprocess = new Process();
        gdbserprocess.StartInfo.UseShellExecute = false;
        gdbserprocess.StartInfo.FileName = gdbserpatch;
        gdbserprocess.StartInfo.Arguments = gdbseropt;
        gdbserprocess.Start();
        Thread.Sleep(2000);

        argstr = argstr.Replace("-gdb ", "");
        mpro.StartInfo.FileName = gdbpatch;
        mpro.StartInfo.Arguments = argstr + gdbopt;
        mpro.StartInfo.UseShellExecute = false;        
        mpro.Start();
        
        mpro.WaitForExit();
        mpro.Close();

        //
        while (gdbserprocess.HasExited == false)
        {
          gdbserprocess.Kill();
          Thread.Sleep(500);
          Console.Write(gdbserprocess.Id.ToString() + "\n");
        }
        gdbserprocess.Close();
        return;
      }
        //cc
      else if (argstr.IndexOf("-make") < 0)
      {
        if (objft == "c" || objft == "cpp")
        {
          //mpro.StartInfo.RedirectStandardOutput = true;
          //mpro.StartInfo.UseShellExecute = false;
          mpro.StartInfo.FileName = ccpatch;
          mpro.StartInfo.Arguments = argstr + ccopt;
          //mpro.WaitForExit();
          //
          //tmpstr1 = mpro.StandardOutput.ReadToEnd();
          //string[] tmpstrarr;
          //tmpstrarr = tmpstr1.Split(new string[]{"\n"},StringSplitOptions.None);
          //tmpstr1 = tmpstrarr[0];
          //foreach (string str1 in tmpstrarr)
          //{
          //  if (str1.Substring(0, 1) == " ")
          //    tmpstr1 += str1;
          //  else
          //    tmpstr1 = tmpstr1 + "\n" + str1;
          //}
          //
          //Console.Write("积极"+tmpstr1);
        }
        else if (objft == "asm")
        {
          mpro.StartInfo.FileName = asmpatch;
          mpro.StartInfo.Arguments = argstr + asmopt;
        }
        else
        {
          Console.Write(argstr + "\n");
          Console.Write(objft + "文件类型错误\n");
          return;
        }
      }
      //
      //Console.Write(mpro.StartInfo.Arguments + "\n");
      //
      mpro.StartInfo.UseShellExecute = false;
      mpro.Start();
      mpro.WaitForExit();
      mpro.Close();
    }
  }
}
